﻿
using System;
using System.IO;
using System.Security.Cryptography;

namespace GDFlacTool.Common
{
    public static class HashChecker
    {
        private static byte[] ConvertStringToByteArray(string data)
        {
            return (new System.Text.UnicodeEncoding()).GetBytes(data);
        }

        private static FileStream GetFileStream(string pathName)
        {
            return (new FileStream(pathName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
        }

        public static string GetSHA1Hash(string pathName)
        {
            string result = string.Empty;

            SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
                
            try
            {
                FileStream fileStream = GetFileStream(pathName);
                byte[] bytes = sha1Hasher.ComputeHash(fileStream);
                fileStream.Close();

                string hashData = BitConverter.ToString(bytes);
                hashData = hashData.Replace("-", "");
                result = hashData;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            return result;
        }

        public static string GetMD5Hash(string pathName)
        {
            string result = string.Empty;

            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();

            try
            {
                FileStream fileStream = GetFileStream(pathName);
                byte[] bytes = md5Hasher.ComputeHash(fileStream);
                fileStream.Close();

                string hashData = BitConverter.ToString(bytes);
                hashData = hashData.Replace("-", "");
                result = hashData;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            return result;
        }
    }
}